package com.woniu.smart.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.smart.dto.BuildingDTO;
import com.woniu.smart.entity.Building;
import com.woniu.smart.service.BuildingService;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.BuildingVo;
import com.woniu.smart.vo.EChartsBuildingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼栋的控制层
 *
 * @author 晨星
 * @date 2022/10/20/14:32
 */
@RestController
@RequestMapping("buildings")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    /**
     * 查询全部楼宇
     *
     * @author 晨星
     * @date 2022/10/20 14:38
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("queryAll")
    public Result queryAllBuilding(@RequestBody BuildingDTO buildingDTO){
        List<BuildingVo> buildingVoList= buildingService.selectAll(buildingDTO);
        PageInfo<BuildingVo> buildingVoPageInfo=new PageInfo<>(buildingVoList);
        return Result.ok().data("list",buildingVoPageInfo);
    }

    /**
     * 根据id查询楼栋
     *
     * @author 晨星
     * @date 2022/10/20 21:40
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("{id}")
    public Result queryByBuildingId(@PathVariable Long id){
        Building building = buildingService.selectByPrimaryKey(id);
        return Result.ok().data("building",building);
    }

    /**
     * 新增楼栋信息
     *
     * @author 晨星
     * @date 2022/10/21 14:31
     * @param building 楼栋的实体类
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping("")
    public Result insertBuilding(@RequestBody Building building){
        buildingService.insertSelective(building);
        return Result.ok();
    }

    /**
     * 修改楼栋信息
     *
     * @author 晨星
     * @date 2022/10/20 21:44
     * @param building 楼栋的实体类
     * @return com.woniu.smart.util.Result
     **/
    @PutMapping("")
    public Result updateBuilding(@RequestBody Building building){
        buildingService.updateByPrimaryKeySelective(building);
        return Result.ok();
    }

    /**
     * 根据id删除楼栋
     *
     * @author 晨星
     * @date 2022/10/21 14:31
     * @param id 主键id
     * @return com.woniu.smart.util.Result
     **/
    @DeleteMapping("{id}")
    public Result deleteBuildingById(@PathVariable Long id){
        buildingService.deleteByPrimaryKey(id);
        return Result.ok();
    }

    /**
     * 根据id批量删除楼栋
     *
     * @author 晨星
     * @date 2022/10/24 17:31
     * @param buildingIds 批量的楼栋主键id
     * @return com.woniu.smart.util.Result
     **/
    @PostMapping ("deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> buildingIds){
        buildingService.deleteBatch(buildingIds);
        return Result.ok();
    }

    /**
     * @Description 根据楼宇id查询包含的楼层数
     * @Parm a
     * @Retrn a
     * @Author WangMr
     * @Date Create by 2022/10/31 16:36
     */
    @GetMapping ("group")
    public Result selectGroupByBuilding(){
        List<EChartsBuildingVO> list = buildingService.selectGroupByBuilding();
        return Result.ok().data("buildings",list);
    }

}
